import csv
import json
import re

import requests

url = ''

f = open('财经.txt',mode='a',encoding='utf-8',newline='')
csv_writer = csv.DictWriter(f,fieldnames=[
    '日期',
    '开盘',
    '最高',
    '最低',
    '收益',
    '成交额'
])
csv_writer.writeheader()

response = requests.get(url=url)

info_list = re.findall('{(.*?)}',re,response.text)

for index in info_list:
    index_dict = '{' + index + '}'
    json_data = json.loads(index_dict)
    dit = {
        '日期' : json_data['d'],
        '开盘' : json_data['a'],
        '最高' : json_data['h'],
        '最低' : json_data['l'],
        '收益' : json_data['c'],
        '成交额' : json_data['v']
    }
    csv_writer.writerow(dit)

